package com.example.happiness.service.admin.Impl;

import com.example.happiness.constant.Message;
import com.example.happiness.mapper.CategoryMapper;
import com.example.happiness.mapper.ServeMapper;
import com.example.happiness.pojo.Category;
import com.example.happiness.pojo.Serve;
import com.example.happiness.pojo.admindto.UcategoryDTO;
import com.example.happiness.result.Result;
import com.example.happiness.service.admin.AdminCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class AdminCategoryServiceImpl implements AdminCategoryService {

    @Autowired
    CategoryMapper categoryMapper;
    @Autowired
    ServeMapper serveMapper;

    public void addCategory(Category category) {
        category.setState(Message.ONE);
        category.setCreatetime(LocalDateTime.now());
        categoryMapper.insert(category);
    }

    public List<Category> selectCategory() {
        return categoryMapper.selectCategory();
    }

    public Result deleteCategory(Integer cid) {
        Serve serve = serveMapper.getserve(cid);
        if (serve != null){
            return Result.error("该分类下有服务，无法删除");
        }
        categoryMapper.deleteByPrimaryKey(cid);
        return Result.success();
    }

    public Result updatestate(UcategoryDTO ucategoryDTO) {
        Serve serve = serveMapper.getserve(ucategoryDTO.getCid());
        if (ucategoryDTO.getState() == 0){
            if (serve != null){
                return Result.error("该分类下有服务，无法禁用");
            }
        }
        categoryMapper.updatestate(ucategoryDTO);
        return Result.success();
    }
}
